
cmake_minimum_required(VERSION 3.10)

project(MyModules)

set(ModulesName MyModule)
set(ModulesDir modules)
set(ModulesCMakeDir "${ModulesDir}/lib/cmake/${ModulesName}")
set(ModulesVersionFile "${PROJECT_BINARY_DIR}/${ModulesName}Version.cmake")

add_subdirectory(libA)
add_subdirectory(libB)

# EXPORT 用来导出Module的 *.cmake 配置文件，
# 一个 Module 可以设置多个 Library, 通过 install install(TARGETS libA EXPORT ${ModulesName}
install(
        EXPORT ${ModulesName}
        FILE ${ModulesName}.cmake
        DESTINATION "${ModulesCMakeDir}" # 一般安装路径会设置在此处
        NAMESPACE My::
        COMPONENT ${ModulesName}
)


set(VERSION_MAJOR "1")
set(VERSION_MINOR "2")
set(VERSION_PATCH "0")

include(CMakePackageConfigHelpers)
write_basic_package_version_file(
        ${ModulesVersionFile}
        VERSION ${VERSION_MAJOR}.${VERSION_MINOR}.${VERSION_PATCH}
        COMPATIBILITY SameMajorVersion)

install(FILES
        ${ModulesVersionFile}
        ${CMAKE_CURRENT_SOURCE_DIR}/MyModuleConfig.cmake
        DESTINATION "${ModulesCMakeDir}"
        COMPONENT  ${ModulesName})

add_custom_target(${ModulesName}Install ALL COMMAND ${CMAKE_COMMAND} --install ${CMAKE_BINARY_DIR} --config $<CONFIG>)

add_dependencies(${ModulesName}Install libA libB)